package com.gw.redis.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class RedisZSetUtil {
    @Autowired //自动注入springboot封装的Redis工具类
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 写入缓存
     * @param key
     * @param value
     * @param score
     * @return
     */
    public boolean zAdd(final String key,Object value,double score){
        boolean result=false;
        try {
            redisTemplate.opsForZSet().add(key,value,score);
            result=true;
        }catch (Exception e){
            e.printStackTrace();;
        }
        return result;
    }

    /**
     * 读取缓存
     * @param key
     * @param start
     * @param stop
     * @return
     */
    public Set zRange(final String key, long start, long stop){
        return redisTemplate.opsForZSet().range(key,start,stop);
    }

    /**
     * 读取缓存
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set zRangByScore(String key,double min,double max){
        return redisTemplate.opsForZSet().rangeByScore(key,min,max);
    }

    /**
     * 删除缓存
     * @param key
     * @param value
     * @return
     */
    public boolean zRem(String key,Object value){
        boolean result=false;
        try {
            redisTemplate.opsForZSet().remove(key,value);
            result=true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
